#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <stdio.h>
#include <disk.h>


volatile int process_count = 0;

void halt();

void test1(void);
void test2(void);
void test3(void);
int process1(char c);
int process2(char c);
int process3(char c);
int process4(char c);

void test1(void) {
	char buffer[128];
	int index, inner;
	
	open(DISK0, 0, 0);

	for(index = 0;index < 1024;index++) {
		for(inner = 0;inner < 128;inner++)
			buffer[inner] = index % 127;
		write(DISK0, buffer, index);
	}

	for(index = 0;index < 1024;index++) {
		read(DISK0, buffer, index);
		kprintf("%d\n", buffer[127]);
	}

	close(DISK0);
}

void test() {
	test1();
	test2();
	test3();
}

int main()
{
	int index,inner;
	char obuffer[128];
	int pr1, pr2, pr3, pr4, pr5, pr6, pr7, pr8, pr9;
	open(DISK0, 0, 0);
	open(DISK1, 0, 0);
	
	for(index = 0;index < 1024;index++) {
		for(inner = 0;inner < 128;inner++)
			obuffer[inner] = (char)(index % 128);
		write(DISK0, obuffer, index);
		write(DISK1, obuffer, index);
	}

	resume(pr1 = create(process2, 2000, 30, "Process 1", 1, 'A'));
	resume(pr2 = create(process2, 2000, 30, "Process 2", 1, 'B'));
	resume(pr3 = create(process2, 2000, 30, "Process 3", 1, 'C'));
	resume(pr4 = create(process2, 2000, 30, "Process 4", 1, 'D'));
	resume(pr5 = create(process2, 2000, 30, "Process 5", 1, 'E'));
	resume(pr6 = create(process2, 2000, 30, "Process 6", 1, 'F'));
	resume(pr7 = create(process2, 2000, 30, "Process 7", 1, 'G'));
	resume(pr8 = create(process2, 2000, 30, "Process 8", 1, 'H'));

	while(process_count < 8);
	close(DISK1);
	close(DISK0);

	return 0;
}

int process1(char c) {
	int index, inner;
	char obuffer[128];

	for(index = 0;index < 1024;index++) {
		for(inner = 0;inner < 128;inner++)
			obuffer[inner] = index % 128;

		write(DISK0, obuffer, index);
	}

	process_count++;
	return process_count;
}

int process2(char c) {
	int index, random;
	char ibuffer[128];
	Bool success = TRUE;

	for(index = 0;index < 1024;index++) {
		random = rand() % 1024;
		read(DISK0, ibuffer, random);
		if(random % 128 != (int)ibuffer[0])
			success = FALSE;
	}

	if(success == TRUE)
		kprintf("Process-%c SUCCESS\n", c);
	else
		kprintf("Process-%c FAIL\n", c);

	process_count++;
	return process_count;
}

int process3(char c) {
	int index, inner;
	char obuffer[128];

	for(index = 0;index < 1024;index++) {
		obuffer[0] = index % 128;
		write(DISK1, obuffer, index);
	}
	return ++process_count;
}

int process4(char c) {
	int index;
	char ibuffer[128];

	for(index = 0;index < 1024;index++) {
		read(DISK1, ibuffer, index);
//		if(index % 128 != (int)ibuffer[0])
//			kprintf("P%c FAIL %d %d\n", c, index % 128, ibuffer[0]);
//		else
//			kprintf("P%c SUCCESS %d\n", c, ibuffer[0]);
	}
	return ++process_count;
}

void test2(void) {
	int index;
	char ibuffer[128];
	char obuffer[128];
	disk_operation ops[9];
	
	ops[0].type = READ_OPERATION; ops[0].block_no = 300;
	ops[1].type = READ_OPERATION; ops[1].block_no = 155;
	ops[2].type = READ_OPERATION; ops[2].block_no = 1000;
	ops[3].type = READ_OPERATION; ops[3].block_no = 1;
	ops[4].type = READ_OPERATION; ops[4].block_no = 954;
	ops[5].type = READ_OPERATION; ops[5].block_no = 400;
	ops[6].type = READ_OPERATION; ops[6].block_no = 123;
	ops[7].type = READ_OPERATION; ops[7].block_no = 5;
	ops[8].type = READ_OPERATION; ops[8].block_no = 777;

	open(DISK1, 0, 0);
	for(index = 0;index < 9;index++) {
		if(ops[index].type == READ_OPERATION)
			read(DISK1, ibuffer, ops[index].block_no);
		else
			write(DISK1, obuffer, ops[index].block_no);
	}
	close(DISK1);
}


void test3(void) {
	char ibuffer[128];
	char obuffer[128];
	int index;

	for(index = 0;index < 128;index++)
		obuffer[index] = index;

	open(DISK0, 0, 0);
	write(DISK0, obuffer, 2); 
	read(DISK0, ibuffer, 2);
	
	for(index = 0;index < 128;index++)
		kprintf("%d", ibuffer[index]);

	kprintf("\n");

	close(DISK0);
}
